<template>
  <div class="app-container">
    <el-form ref="dataForm" :model="tempUserRole" label-width="120px">
      <el-form-item label="User Account">
        <el-input :disabled="type == 'update'" v-model="tempUserRole.user_account"></el-input>
      </el-form-item>
      <el-form-item label="User Name">
        <el-input :disabled="type == 'update'" v-model="tempUserRole.user_name"></el-input>
      </el-form-item>
      <el-form-item label="Title">
        <el-input :disabled="type == 'update'" v-model="tempUserRole.title"></el-input>
      </el-form-item>
      <el-form-item label="Sex">
        <el-radio-group :disabled="type == 'update'" v-model="tempUserRole.sex" >
          <el-radio v-for="item in  sexList" :key="item" :label="item" :value="item"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="Role">
        <el-select v-model="tempUserRole.role_id" placeholder="please select authorization">
          <el-option v-for="item in  rolesList" :key="item.role_id" :label="item.role_name" :value="item.role_id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-if="type=='create'" type="primary" @click="createData">Create</el-button>
        <el-button v-else type="primary" @click="updateData">Update</el-button>
        <el-button  @click="onCancel">Cancel</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { getAllRoles, updateUserRole } from '@/api/authority'

export default {
  data() {
    return {
      rolesList: null,
      tempUserRole: {
        user_id: 0,
        role_id: 0,
        user_name: '',
        role_name: '',
        user_account: '',
        sex: '',
        title: ''
      },
      rules: {
        user_account: [{ required: true, message: 'user account is required', trigger: 'change' }],
        user_name: [{ required: true, message: 'user name is required', trigger: 'change' }],
        role: [{ required: true, message: 'role is required', trigger: 'change' }]
      },
      type: 'create',
      sexList: ['male', 'female']
    }
  },
  created: function() {
    if (this.$route.params.type !== undefined) {
      this.tempUserRole = this.$route.params.userRole
      this.type = this.$route.params.type
    }
    this.getRolesList()
  },
  methods: {
    getRolesList() {
      getAllRoles().then(response => {
        this.rolesList = response
      })
    },
    createData() {
      this.$notify({
        title: 'Error',
        message: 'Function to be developed',
        type: 'error',
        duration: 2000
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateUserRole(this.tempUserRole).then(() => {
            this.$notify({
              title: 'Success',
              message: 'Modify Success!',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    onCancel() {
      this.$refs['dataForm'].resetFields()
      this.$notify({
        title: 'Cancel',
        message: 'Cancel Success!',
        type: 'success',
        duration: 2000
      })
      this.$router.push({
        name: 'QueryAuth', params: {
        }
      })
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

